<template>
  <div class="app-container">
    <el-form
      :model="data"
      :rules="setRules"
      ref="ruleForm"
      label-width="100px"
      width="500px"
    >
      <el-form-item label="用户名">
        <el-input v-model="data.name" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="旧密码" prop="oldLoginPwd">
        <el-input
          v-model="data.oldLoginPwd"
          placeholder="请输入旧密码"
          type="password"
        ></el-input>
      </el-form-item>

      <el-form-item label="新密码" prop="loginPwd">
        <el-input
          v-model="data.loginPwd"
          placeholder="请输入新密码"
          type="password"
        ></el-input>
      </el-form-item>

      <el-form-item label="新密码确认" prop="loginPwdConfirm">
        <el-input
          v-model="data.loginPwdConfirm"
          placeholder="请确认新密码"
          type="password"
        ></el-input>
      </el-form-item>

      <el-button type="primary" style="margin-top: 15px" @click="handleClick"
        >修改</el-button
      >
      <el-button type="danger" style="margin-top: 15px" @click="handleBack"
        >返回</el-button
      >
    </el-form>
  </div>
</template>

<script>
import { setUser, getInfoApi } from "@/api/user";
export default {
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入新密码"));
      } else if (value !== this.data.loginPwd) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };

    return {
      data: {
        name: "",
        id: "",
        loginId: "",
        oldLoginPwd: "",
        loginPwd: "",
        loginPwdConfirm: "",
      },
      setRules: {
        oldLoginPwd: [
          { required: true, trigger: "blur", message: "请输入旧的密码" },
        ],
        loginPwd: [
          { required: true, trigger: "blur", message: "请输入新的密码" },
        ],
        loginPwdConfirm: [
          { required: true, trigger: "blur", message: "请再次输入新密码" },
          { validator: validatePass2, trigger: "blur" },
        ],
      },
    };
  },

  created() {
    getInfoApi().then((res) => (this.data = res.data));
  },
  methods: {
    handleClick() {
      //判断是否都已经输入
      if (this.data.name && this.data.loginPwd && this.data.oldLoginPwd) {
        //判断新密码和确认密码是否一致
        if (this.data.loginPwd !== this.data.loginPwdConfirm) {
          this.$message("两次密码输入不一致");
          return;
        }

        setUser(this.data).then((res) => {
          if (typeof res === "string") {
            //密码不正确
            const resp = JSON.parse(res);
            this.$message.error(resp.msg);
          } else {
            this.$message.success("修改密码成功，请重新登录");
            //退出登录
            this.$store.dispatch("user/logout").then(() => {
              this.$router.push(`/login?redirect=${this.$route.fullPath}`);
            });
          }
        });
      } else {
        this.$message.error("请完善所有信息");
      }
    },
    handleBack() {
      this.$router.push("/");
    },
  },
};
</script>

<style scoped>
.app-container {
  width: 500px;
}
</style>
